# game.rb
# created by: Dan Alexander
# date: 3/5/2011
# license: New BSD License
#
# This is the main program for the client.
# This project is a part of "Adventures of 5 nachos awesomeness",
# a roguelike project started during 7DRL 2011 competition.

#!/usr/bin/env ruby

main_dir = File.dirname(__FILE__)
SDL_PATHS = [ File.join( main_dir, "libs" ) ]

require "rubygems"
require "rubygame"
require_relative "resources"
require_relative "gamestate"
require_relative "ingamestate"
require_relative "map"
require_relative "menustate"
require_relative "chargamestate"
require_relative "messagestate"
require "json"
require "socket"
 
include Rubygame

#default the debug option to false
$debug = false

#global should_run variable. setting this to false will end the game
$should_run = true

$player_name = ""
$player_image = "warrior"
$player_stats = Hash.new

#check command line arguments
ARGV.each do |a|
	if a == "-d" || a == "--debug"
		$debug = true
	end
end

File.open("server.conf","r") do |file|
	while line = file.gets
		line2 = line.strip!
		if line2 != nil then line = line2 end
		key = line.split("=")[0]
		value = line.split("=")[1]
		if key == "host" then $hostname = value end
		if key == "port" then $port = Integer(value) end
	end
end

#setup the main game screen
@screen = Screen.open [ 640, 480]
@screen.title = "Adventures of 5 Nachos Awesomeness"

#initialize the resource manager and have it load
#the resources
$resman = ResourceManager.new("resources.dat");

#game clock
@clock = Clock.new
@clock.target_framerate = 60

# Ask Clock.tick() to return ClockTicked objects instead of the number of
# milliseconds that have passed:
@clock.enable_tick_events

#setup the gamestate management
$gamestates = Hash.new
$gamestates["ingame"] = InGameState.new(@screen) #add the ingame state
$gamestates["mainmenu"] = MenuGameState.new(@screen) #add the main menu state
$gamestates["charselect"] = CharacterSelectState.new(@screen) #add the character selection game state
$gamestates["message"] = MessageGameState.new(@screen) # add the message game state
$currentstate = "mainmenu" #set the starting game state

#setup events
$event_queue = EventQueue.new
$event_queue.enable_new_style_events

#main game loop
while $should_run do
	#how many seconds have passed since last
	seconds_passed = @clock.tick().seconds
	
	#if in debug mode, show the FPS in the titlebar
	#if $debug then @screen.title = @clock.framerate.to_int().to_s() + " FPS" end
	@screen.title = @clock.framerate.to_int().to_s() + " FPS"
	
	#update the current game state
	$gamestates[$currentstate].update
	
	#draw the current game state
	$gamestates[$currentstate].draw

	@screen.flip
	
	
	
	sleep(0.001)
end
